<?php

class Product
{

    const CODEDRIVE = "codedrive";
    const RIO       = "rio";
                                               
    private static $data = array();
    private static $actual = array();

    private static $products = null;
    private static $latestProducts = null;

    private function __construct() {}

    public static function __construct_static()
    {
        self::$data = Core::Config()->products;
        self::$actual = Core::Config()->actual_products;
    }

    public static function GetName( $prodId )
    {
        $name = '';
        if ( isset( self::$data[$prodId]) )
        {
            $name = self::$data[$prodId];
        }

        if ( empty($name) )
        {
            $prodInfo = self::GetLatestProductInfo( $prodId );
            if ( !empty($prodInfo) )
            {
                $name = $prodInfo->name;
            }
        }
        return $name;
    }

    public static function GetBrand($prodId)
    {
        return $prodId == Product::CODEDRIVE ? 'CodeDrive' : 'iSpring';
    }  

    public static function IsCorrect( $prodId )
    {
        return array_key_exists($prodId, self::$data);
    }

    public static function IsFree( $prodId )
    {
        $product = self::GetLatestProductInfo($prodId);

        return $product ? $product->isFree : false;
    }

    public static function NormalizeData( ProductInfo &$product )
    {
        $product->size      = Util::GetSizeInMB($product->size);
        $product->size      = Util::GetNumberAsString($product->size, 0, 2);
        $product->date      = Util::NormalizeDate($product->date, "%Y-%m-%d", "%d %B %Y");
    }

    /**
     * @param string $prodId
     * @return ProductInfo
     */
    public static function GetActualProdId( $prodId )
    {
        return isset(self::$actual[$prodId]) ? self::$actual[$prodId] : $prodId;
    }

    /**
     * @param string $prodId
     * @return ProductInfo
     */
    public static function GetLatestProductInfo( $prodId )
    {
        $products = self::GetLatestProducts();
        if ( isset($products[$prodId]) )
        {
            return $products[$prodId];
        }
        return null;
    }

    public static function GetLatestProductsInfo()
    {
        $products = self::GetLatestProducts();
        return $products;
    }

    public static function GetProductsInfo( $prodId = null )
    {
        $products = self::GetProducts();
        if ( !is_null($prodId) )
        {
            if ( !isset($products[$prodId]) )
            {
                return null;
            }
            $products = $products[$prodId];
        }
        return $products;
    }

    public static function GetArchiveProductsInfo( $prodId )
    {
        $products = self::GetProductsInfo( $prodId );
        $isActualProduct = (self::GetActualProdId( $prodId ) == $prodId);
        if ( !$isActualProduct )
        {
            return $products;
        }
        else
        {
            return $isActualProduct ? array_slice($products, 0, count($products) - 1) : $products;
        }
    }

    /**
     * Returns an array of products which serial provided is valid for
     * @param LicenseInfo $licenseInfo
     * @return array
     */
    public static function GetProductsInfoByLicense( $licenseInfo )
    {
        $result = array();
        if (!$licenseInfo)
        {
            return $result;
        }

        if ($licenseInfo->license == License::SUBSCRIPTION)
        {
            return self::GetProductsInfo($licenseInfo->product);
        }
        else if ($licenseInfo->license == License::BUSINESS || $licenseInfo->license == License::OEM
            || $licenseInfo->license == License::TRIAL && Product::IsFree($licenseInfo->product))
        {
            $licenseMajorVersion = Util::NormalizeVersion($licenseInfo->version, 1);
            $products = self::GetProductsInfo($licenseInfo->product);

            foreach ($products as $product)
            {
                if ($licenseMajorVersion >= '4' && $product->shortName != Product::IS_SDK && $product->shortName != Product::IS_PLATFORM)
                {
                    // serial is valid for all products with the same major version
                    $productMajorVersion = Util::NormalizeVersion($product->version, 1);

                    if ( Util::CompareVersions($licenseMajorVersion, $productMajorVersion) == 0)
                    {
                        $result[] = $product;
                    }
                }
                elseif ( Util::CompareVersions($licenseInfo->version, $product->version) == 0)
                {
                    $result[] = $product;
                }
            }
        }
        return $result;
    }

    public static function GetProductInfoForVersion( $prodId, $version )
    {
        $products = self::GetProductsInfo($prodId);

        foreach ($products as $product)
        {
            if (Util::CompareVersions($product->version, $version) == 0)
            {
                return $product;
            }
        }
    }

    public static function GetIsOnlineProductInfo()
    {
        $product = new ProductInfo();
        $product->name = self::IS_ONLINE_NAME;
        $product->shortName = self::IS_ONLINE;

        return $product;
    }

    public static function GetProductVersions( $prodId, $start = null, $excluding = array() )
    {
        $versions = array();
        $products = self::GetProductsInfo( $prodId );
        if ($products)
        {
            foreach ( $products as $product )
            {
                if ($product->version >= $start && !in_array($product->version, $excluding))
                {
                    $versions[] = $product->version;
                }
            }
        }

        return $versions;
    }

    public static function GetTrialLicensePageUrl(ProductInfo $productInfo)
    {
        $url = '';
        if ($productInfo->shortName == self::IS_QUIZ_MAKER_FREE)
        {
            $url = '/free-quiz-maker/download.html';
        }
        else if ($productInfo->shortName == self::IS_ONLINE)
        {
            $url = '/ispring-online/get-free-trial.html';
        }
        else if ($productInfo->shortName == self::IS_FREE)
        {
            $url = '/free_powerpoint_to_flash_converter/download.html';
        }
        else if ($productInfo->shortName == self::IS_SUITE_FREE)
        {
            $url = '/free-elearning-suite/download.html';
        }
        else if ($productInfo->shortName == self::IS_CONV)
        {
            $url = '/ispring-converter/download.html';
        }
        else
        {
            $url = '/ispring-' . $productInfo->GetShortCode() . '/download.html';
        }

        if ( !Request::IsSecure() )
        {
            $url = 'https://' . Request::GetHost() . $url;
        }
        return $url;
    }

    public static function IsDesktop( $prodId )
    {
        return !in_array( $prodId, array(
            Product::IS_CASE_ANALYSIS,
            Product::IS_REMOTE_ASSISTANCE,
            Product::IS_ONLINE
        ) );
    }

    private static function GetProducts()
    {
        if ( is_null(self::$products) )
        {
            self::$products = array();
            $products = iSpringServices::GetProductsInfo();
            if ( $products )
            {
                foreach ( $products as $productInfo )
                {
                    self::NormalizeData( $productInfo );
                    $shortName = $productInfo->shortName;
                    if ( !isset(self::$products[$shortName]) )
                    {
                        self::$products[$shortName] = array();
                    }
                    self::$products[$shortName][] = $productInfo;
                }
            }
        }
        return self::$products;
    }

    private static function GetLatestProducts()
    {
        if ( is_null(self::$latestProducts) )
        {
            self::$latestProducts = array();
            $products = iSpringServices::GetActualProducts();
            if ( $products )
            {
                foreach ( $products as $productInfo )
                {
                    self::NormalizeData( $productInfo );
                    self::$latestProducts[$productInfo->shortName] = $productInfo;
                }
            }
        }
        return self::$latestProducts;
    }

    public static function IsProductHasOSRequirementsOnly( ProductInfo $productInfo)
    {
        return ($productInfo->shortName == self::IS_PLATFORM)
                || ($productInfo->shortName == self::IS_QUIZ_MAKER)
                || ($productInfo->shortName == self::IS_QUIZ_MAKER_FREE)
                || ($productInfo->shortName == self::IS_KINETICS
                || ($productInfo->shortName == self::IS_CONV) );
    }
};

?>
